/**
 * Copyright &copy; 2016 splat  All rights reserved.
 */
package com.contractpage.common.persistence;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;

import com.common.seq.sql.SqlSeqUtil;
import com.contractpage.util.StringUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.user.entity.emp.EmpVO;

/**
 * 数据Entity类
 * 
 * @author splat
 * @version 2014-05-16
 */
public abstract class DataEntity<T> extends BaseEntity<T> {

	private static final long serialVersionUID = 1L;

	protected String remarks; // 备注
	protected EmpVO createBy; // 创建者
	protected Date createDate; // 创建日期
	protected EmpVO updateBy; // 更新者
	protected Date updateDate; // 更新日期
	protected String delFlag; // 删除标记（1：正常；0：删除）
	protected String platType; // 平台标识

	public String getPlatType() {
		return platType;
	}

	public void setPlatType(String platType) {
		this.platType = platType;
	}

	public DataEntity() {
		super();
		this.delFlag = DEL_FLAG_NORMAL;
	}

	public DataEntity(String id) {
		super(id);
	}

	public void preInsert(){
		String tableName = getClass().getSimpleName();
		String name = "contractpage_" + StringUtils.toUnderScoreCase(tableName);
		String id = String.valueOf(SqlSeqUtil.get(name));
		setId(id);
	}
	
	/**
	 * 插入需要手动调用
	 * 
	 * @param tablename
	 */
	@Override
	public void preInsert(String key) {
		if(StringUtils.isEmpty(key)){
			preInsert();
		}
		this.createDate = new Date();
		this.updateDate = new Date();
	}

	/**
	 * 更新之前执行方法，需要手动调用
	 */
	@Override
	public void preUpdate() {
		/*EmpVO user = UserUtils.getUser();
		if (StringUtils.isNotBlank(user.getCode())) {
			this.updateBy = user;
		}*/
		this.updateDate = new Date();
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getUpdateDate() {
		return updateDate;
	}

	public EmpVO getCreateBy() {
		return createBy;
	}

	public void setCreateBy(EmpVO createBy) {
		this.createBy = createBy;
	}

	public EmpVO getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(EmpVO updateBy) {
		this.updateBy = updateBy;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	@JsonIgnore
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	public static String underscoreName(String name) {
		StringBuilder result = new StringBuilder();
		if (name != null && name.length() > 0) {
			// 将第一个字符处理成大写
			result.append(name.substring(0, 1).toUpperCase());
			// 循环处理其余字符
			for (int i = 1; i < name.length(); i++) {
				String s = name.substring(i, i + 1);
				// 在大写字母前添加下划线
				if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
					result.append("_");
				}
				// 其他字符直接转成大写
				result.append(s.toUpperCase());
			}

		}
		return result.toString();
	}
	public String isNumber(String num) {
		float f = Float.parseFloat(num);
		int fi = (int) f;
		if (f == fi) {
			return fi+"";
		}
		return f+"";
	}
	public String format(BigDecimal decimal,int i){
		if(decimal == null){
			return "";
		}
		String str = "0.";
		for(int j = 0  ; j < i ; j++){
			str += "0";
		}
		DecimalFormat df = new DecimalFormat(str);
		NumberFormat percent = NumberFormat.getCurrencyInstance();  //建立百分比格式化引用 
 	    percent.setMaximumFractionDigits(i); //百分比小数点最多3位 
 	    BigDecimal big = new BigDecimal(df.format(decimal));
 	    String st = percent.format(big);
    	return st.substring(1,st.length());
	}
}
